package edu.avans.hartigehap.domain;

import edu.avans.hartigehap.repository.WebDishRepository;



public class WebPizzaFactory {

	private static WebPizzaFactory instance = null;

	private WebPizzaFactory() {

	}

	public Long addDish(WebMenuItem item, WebOrder order, WebDishRepository webDishRepository) {
		WebDish dish = new WebPizza((MenuPizza) item);
		dish = webDishRepository.save(dish);
		Long id = dish.getId();
		WebDish dish2 = order.addOrderItem(dish);
		if(!dish2.equals(dish)) {
			webDishRepository.delete(dish);
			id = dish2.getId();
		}

		return id;
	}

	public Long addTopping(WebMenuItem topping, WebDish dish, WebOrder order, WebDishRepository webDishRepository) {

		order.deleteOrderItem(dish);

		WebDish t = new WebTopping((MenuTopping) topping);
		((WebPizzaDecorator) t).decorate(dish);
		t = webDishRepository.save(t);

		order.addOrderItem(t);
		return t.getId();
	}

	static {
		instance = new WebPizzaFactory();
	}

	public static WebPizzaFactory getInstance() {
		return instance;
	}

}
